package com.caowei.weicimpress.weicimpress.widget;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by caoweiPc on 2014/8/28.
 */
public class AlertDialog extends Dialog {

    protected AlertDialog(Context context, int theme) {
        super(context, theme);
    }

   public   static class Builder{
       private final AlertParams P;
       private int mTheme;
       private final int SCREEN_W;
       public Builder(Context context,int theme) {
            this.P=new AlertParams(context);
            this.mTheme=theme;
            SCREEN_W=P.mContext.getResources().getDisplayMetrics().widthPixels;
       }

       public Context getContext() {
           return P.mContext;
       }

       public Builder setView(View view) {
           P.mView = view;
           return this;
       }


       public AlertDialog createDialog(){
           final AlertDialog dialog = new AlertDialog(P.mContext, mTheme);
           P.apply(dialog);
           Window dialogWindow =dialog.getWindow();
           WindowManager.LayoutParams params= dialogWindow.getAttributes();
           dialogWindow.setGravity(Gravity.CENTER);
           params.width = (int) (SCREEN_W*0.8);
//           params.alpha = 0.9f;
           dialogWindow.setAttributes(params);
           return dialog;
       }
    }

    private static class AlertParams{
        public final Context mContext;
        public View mView;
        public AlertParams(Context mContext) {
            this.mContext = mContext;
        }
        public void apply(AlertDialog dialog) {
            if(mView!=null){
                dialog.setContentView(mView);
            }
        }
    }
}
